package com.hairline.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hairline.bean.*;
import com.hairline.bean.bo.ShipOrderBO;
import com.hairline.bean.vo.*;
import com.hairline.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class MarketServiceImpl implements MarketService {

    @Autowired
    RegionMapper regionMapper;

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderGoodsMapper orderGoodsMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    IssueMapper issueMapper;

    @Autowired
    KeywordMapper keywordMapper;

    @Override
    public List<Region> regionList() {
        RegionExample regionExample = new RegionExample();
        return regionMapper.selectByExample(regionExample);
    }

    @Override
    public BrandListVO brandList(Integer page, Integer limit, Integer id, String name, String sort, String order) {
        BrandListVO brandListVO = new BrandListVO();

        if (page != null && limit != null) {
            PageHelper.startPage(page, limit);
        }

        BrandExample example = new BrandExample();
        BrandExample.Criteria criteria = example.createCriteria();
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (order != null) {
            example.setOrderByClause(sort + " " + order);
        }
        criteria.andDeletedEqualTo(false);

        List<Brand> brands = brandMapper.selectByExample(example);
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);
        long total = pageInfo.getTotal();

        brandListVO.setItems(brands);
        brandListVO.setTotal(total);
        return brandListVO;
    }

    @Override
    public int brandCreate(Brand brand) {
        brand.setAddTime(new Date());
        return brandMapper.insertSelective(brand);
    }

    @Override
    public int brandUpdate(Brand brand) {
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        criteria.andIdEqualTo(brand.getId());
        int i = brandMapper.updateByExampleSelective(brand, brandExample);
        return i;
    }

    @Override
    public int brandDelete(Brand brand) {
        brand.setDeleted(true);
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        criteria.andIdEqualTo(brand.getId());
        int i = brandMapper.updateByExampleSelective(brand, brandExample);
        return i;
    }

    @Override
    public List<Category> categoryList() {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<Category> list = categoryMapper.selectByExample(categoryExample);
        return list;
    }

    @Override
    public int categoryCreate(Category category) {
        int i = categoryMapper.insertSelective(category);
        return i;
    }

    @Override
    public List<CategoryL1VO> categoryL1() {
        return categoryMapper.categoryL1();
    }

    @Override
    public int categoryUpdate(Category category) {
        int i = categoryMapper.updateByPrimaryKeySelective(category);
        return i;
    }

    @Override
    public int categoryDelete(Category category) {
        category.setDeleted(true);
        int i = categoryMapper.updateByPrimaryKeySelective(category);
        return i;
    }

    @Override
    public OrderListVO orderList(Integer page, Integer limit, String sort, String order, Short[] orderStatusArray,
                                 String orderSn, Integer userId) {
        OrderListVO orderListVO = new OrderListVO();

        //排序
        OrderExample orderExample = new OrderExample();
        orderExample.setOrderByClause(sort + " " + order);

        //动态条件
        OrderExample.Criteria criteria = orderExample.createCriteria();
        if (page != null && limit != null) {
            PageHelper.startPage(page, limit);
        }
        if (orderStatusArray != null) {
            List<Short> shorts = Arrays.asList(orderStatusArray);
            criteria.andOrderStatusIn(shorts);
        }
        if (orderSn != null && !"".equals(orderSn)) {
            criteria.andOrderSnEqualTo(orderSn);
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        criteria.andDeletedEqualTo(false);

        List<Order> list = orderMapper.selectByExample(orderExample);
        PageInfo<Order> pageInfo = new PageInfo<>(list);
        Long total = pageInfo.getTotal();

        orderListVO.setItems(list);
        orderListVO.setTotal(total);
        return orderListVO;
    }

    @Override
    public OrderDetailVO orderDetail(Integer id) {
        OrderDetailVO orderDetailVO = new OrderDetailVO();

        //orderGoods
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        orderGoodsExample.createCriteria().andOrderIdEqualTo(id);
        orderDetailVO.setOrderGoods(orderGoodsMapper.selectByExample(orderGoodsExample));

        //order
        Order order = orderMapper.selectByPrimaryKey(id);
        orderDetailVO.setOrder(order);

        //user
        orderDetailVO.setUser(userMapper.selectByPrimaryKey(order.getUserId()));

        return orderDetailVO;
    }

    @Override
    public int orderShip(ShipOrderBO shipOrderBO) {
        int i = orderMapper.shipOrder(shipOrderBO);
        return i;
    }

    @Override
    public IssueListVO issueList(Integer page, Integer limit, String question, String sort, String order) {
        IssueListVO issueListVO = new IssueListVO();
        IssueExample issueExample = new IssueExample();
        IssueExample.Criteria criteria = issueExample.createCriteria();

        //查询条件
        if (question != null) {
            criteria.andQuestionLike("%" + question + "%");
        }
        criteria.andDeletedEqualTo(false);

        //分页
        PageHelper.startPage(page, limit);

        //排序
        issueExample.setOrderByClause(sort + " " + order);

        //查询
        List<Issue> issues = issueMapper.selectByExample(issueExample);
        Long total = new PageInfo<>(issues).getTotal();

        issueListVO.setItems(issues);
        issueListVO.setTotal(total);
        return issueListVO;
    }

    @Override
    public int issueCreate(Issue issue) {
        int i = issueMapper.insertSelective(issue);
        return i;
    }

    @Override
    public int issueUpdate(Issue issue) {
        int i = issueMapper.updateByPrimaryKeySelective(issue);
        return i;
    }

    @Override
    public int issueDelete(Issue issue) {
        issue.setDeleted(true);
        int i = issueMapper.updateByPrimaryKeySelective(issue);
        return i;
    }

    @Override
    public KeywordListVO keywordList(Integer page, Integer limit, String keyword, String url, String sort, String order) {
        KeywordListVO keywordListVO = new KeywordListVO();

        PageHelper.startPage(page, limit);
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        if (keyword != null && !"".equals(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (url != null && !"".equals(url)) {
            criteria.andUrlLike("%" + url + "%");
        }
        criteria.andDeletedEqualTo(false);
        keywordExample.setOrderByClause(sort + " " + order);

        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        Long total = new PageInfo<>(keywords).getTotal();
        keywordListVO.setItems(keywords);
        keywordListVO.setTotal(total);

        return keywordListVO;
    }

    @Override
    public int keywordCreate(Keyword keyword) {
        int i = keywordMapper.insertSelective(keyword);
        return i;
    }

    @Override
    public int keywordUpdate(Keyword keyword) {
        int i = keywordMapper.updateByPrimaryKeySelective(keyword);
        return i;
    }

    @Override
    public int keywordDelete(Keyword keyword) {
        keyword.setDeleted(true);
        int i = keywordMapper.updateByPrimaryKeySelective(keyword);
        return i;
    }
}
